#include"Calculate.hpp"
#include"Reactor.hpp"
#include"Parser.hpp"
#include"Listener.hpp"
#include<string>
#include<iostream>
#include<memory>

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " serverport" << std::endl;
}

int main(int argc , char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        return 0;
    }

    uint16_t serverport = std::stoi(argv[1]);

    //上层处理任务的模块
    std::unique_ptr<Calculate> cal = std::make_unique<Calculate>();

    //解析模块
    std::unique_ptr<Parser> parser = std::make_unique<Parser>(
        [&cal](Request& req)->Responce {
            return cal->Exec(req);
        }
    );

    //网络模块
    //创建监听套接字
    std::shared_ptr<Connection> listener = std::make_shared<Listener>(serverport);
    listener->SetCB([&parser](std::string& inbuffer)->std::string{
        return parser->Parse(inbuffer);});

    //创建反应堆，将监听套接字添加进反应堆当中
    std::unique_ptr<Reactor> reactor = std::make_unique<Reactor>();
    reactor->AddConnection(listener);

    //派发任务
    reactor->Dispatcher();    
    return 0;
}